package com.abocode.aichat.app.user.app;


import com.abocode.aichat.app.user.constant.UserConstants;
import com.abocode.aichat.app.user.service.UserService;
import com.abocode.aichat.app.user.vo.UserVo;
import com.iflytek.application.common.springjdbc.JdbcTemplateOperation;
import com.iflytek.application.common.springjdbc.Pagination;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by guanxf on 2015/12/16.
 */
@org.springframework.stereotype.Service
@Transactional
public class JdbcUserService  extends  JpaUserService implements UserService {
    @Autowired
    @NonNull
    private JdbcTemplateOperation jdbcTemplateOperation;

    public Pagination findAllWithRandom(Pageable pageable) {
        long total = userRepository.countByType(UserConstants.USER_TYPE_IS_OFFICIAL);
        long totalPage = total / pageable.getPageSize();
        if (totalPage < pageable.getPageNumber()) {
            return null;
        }
        Pagination users = jdbcTemplateOperation.getPaginationList(UserVo.class, "t_b_user t", " 1=1 and t.type=2", "RAND()", pageable.getPageNumber() + 1, pageable.getPageSize());
        return users;
    }
}
